package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.PermissionService;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.hibernate.validator.constraints.NotEmpty;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/role")
public class RoleController {
    @DubboReference
    private RoleService roleService;
    @DubboReference
    private PermissionService permissionService;
    @RequestMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        PageResult pageResult=roleService.findPage(queryPageBean);
        return new Result(true,"角色查询成功",pageResult);
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Role role=roleService.findById(id);
        return new Result(true,"查询权限成功",role);
    }
    @RequestMapping("/findAll")
    public Result findAll(){
        List<Permission> permissions=permissionService.findAll();
        return new Result(true,"查询全部成功",permissions);
    }
    @RequestMapping("/findMenuAll")
    public Result findMenuAll(){
        List<Menu> menus=roleService.findMenuAll();
        return new Result(true,"查询全部成功",menus);
    }
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id){
        Integer[] permissionIds=permissionService.findPermissionIdsByRoleId(id);
        return new Result(true,"回显权限id成功",permissionIds);
    }
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id){
        Integer[] menuIds=roleService.findMenuIdsByRoleId(id);
        return new Result(true,"回显权限id成功",menuIds);
    }
    @RequestMapping("/add")
    public Result add(Integer[] permissionIds,Integer[] menuIds,@RequestBody Role role){
        roleService.add(permissionIds,role,menuIds);
        return new Result(true,"添加成功");
    }
    @RequestMapping("/edit")
    public Result edit(Integer[] permissionIds,Integer[] menuIds,@RequestBody Role role){
        roleService.edit(permissionIds,role,menuIds);
        return new Result(true,"修改成功");
    }
    @RequestMapping("/del")
    public Result del(Integer id){
        roleService.del(id);
        return new Result(true,"删除成功");
    }
}
